package cn.iocoder.yudao.module.system.controller.admin.homedata;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.system.controller.admin.homedata.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.homedata.HomeDataDO;
import cn.iocoder.yudao.module.system.service.homedata.HomeDataService;

@Tag(name = "管理后台 - 首页数据")
@RestController
@RequestMapping("/system/home-data")
@Validated
public class HomeDataController {

    @Resource
    private HomeDataService homeDataService;

    @PostMapping("/create")
    @Operation(summary = "创建首页数据")
    @PreAuthorize("@ss.hasPermission('system:home-data:create')")
    public CommonResult<Long> createHomeData(@Valid @RequestBody HomeDataSaveReqVO createReqVO) {
        return success(homeDataService.createHomeData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新首页数据")
    @PreAuthorize("@ss.hasPermission('system:home-data:update')")
    public CommonResult<Boolean> updateHomeData(@Valid @RequestBody HomeDataSaveReqVO updateReqVO) {
        homeDataService.updateHomeData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除首页数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:home-data:delete')")
    public CommonResult<Boolean> deleteHomeData(@RequestParam("id") Long id) {
        homeDataService.deleteHomeData(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除首页数据")
                @PreAuthorize("@ss.hasPermission('system:home-data:delete')")
    public CommonResult<Boolean> deleteHomeDataList(@RequestParam("ids") List<Long> ids) {
        homeDataService.deleteHomeDataListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得首页数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:home-data:query')")
    public CommonResult<HomeDataRespVO> getHomeData(@RequestParam("id") Long id) {
        HomeDataDO homeData = homeDataService.getHomeData(id);
        return success(BeanUtils.toBean(homeData, HomeDataRespVO.class));
    }
    @GetMapping("/homeData")
    @Operation(summary = "获得首页基础数据")
    public CommonResult<Map> homeData() {
        Map homeData = homeDataService.homeData();
        return success(homeData);
    }

    @GetMapping("/page")
    @Operation(summary = "获得首页数据分页")
    @PreAuthorize("@ss.hasPermission('system:home-data:query')")
    public CommonResult<PageResult<HomeDataRespVO>> getHomeDataPage(@Valid HomeDataPageReqVO pageReqVO) {
        PageResult<HomeDataDO> pageResult = homeDataService.getHomeDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HomeDataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出首页数据 Excel")
    @PreAuthorize("@ss.hasPermission('system:home-data:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportHomeDataExcel(@Valid HomeDataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HomeDataDO> list = homeDataService.getHomeDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "首页数据.xls", "数据", HomeDataRespVO.class,
                        BeanUtils.toBean(list, HomeDataRespVO.class));
    }

}
